﻿using System;
using WasmerSharp;

namespace dotnettest
{



    internal class Program
    {

        static void __emscripten_request_animation_frame_loop(InstanceContext ctx, int a, int b)
        {
            Console.Write("----__emscripten_request_animation_frame_loop--\n");
            return;
        }
        static void __emscripten_console_log(InstanceContext ctx, int a)
        {
            Console.Write("----__emscripten_console_log--\n");
            return;
        }
        static int __fd_write(InstanceContext ctx, int a, int b, int c, int d)
        {
            Console.Write("----__fd_write--\n");
            return 0;
        }
        static void __emscripten_memcpy_big(InstanceContext ctx, int a, int b, int c)
        {
            Console.Write("----__emscripten_memcpy_big--\n");
            return;
        }
        delegate void dele__emscripten_request_animation_frame_loop(InstanceContext ctx, int arg1, int arg2);
        delegate void dele__emscripten_console_log(InstanceContext ctx, int arg1);
        delegate int dele__fd_write(InstanceContext ctx, int arg1, int arg2, int c, int d);
        delegate void dele__emscripten_memcpy_big(InstanceContext ctx, int arg1, int arg2,int c);
        static void Main(string[] args)
        {
            byte[] bts = System.IO.File.ReadAllBytes("t_O2.wasm");
            var module = WasmerSharp.Module.Create(bts);

            WasmerSharp.Import[] imports = new WasmerSharp.Import[module.ImportDescriptors.Length];
            for (var i = 0; i < module.ImportDescriptors.Length; i++)
            {
                var desc = module.ImportDescriptors[i];
                if (desc.Kind == WasmerSharp.ImportExportKind.Function)
                {
                    if (desc.Name == "emscripten_request_animation_frame_loop")
                    {
                        imports[i] = new Import(desc.ModuleName, "emscripten_request_animation_frame_loop", new ImportFunction((dele__emscripten_request_animation_frame_loop)(__emscripten_request_animation_frame_loop)));
                    }
                    if (desc.Name == "emscripten_console_log")
                    {
                        imports[i] = new Import(desc.ModuleName, "emscripten_console_log", new ImportFunction((dele__emscripten_console_log)(__emscripten_console_log)));
                    }
                    if (desc.Name == "fd_write")
                    {
                        imports[i] = new Import(desc.ModuleName, "fd_write", new ImportFunction((dele__fd_write)(__fd_write)));
                    }
                    if (desc.Name == "emscripten_memcpy_big")
                    {
                        imports[i] = new Import(desc.ModuleName, "emscripten_memcpy_big", new ImportFunction((dele__emscripten_memcpy_big)(__emscripten_memcpy_big)));
                    }
                    //var func = new WasmerSharp.ImportFunction();
                    //imports[i] = func;
                }
            }
            var inst=module.Instatiate(imports);
            //var instance = new Instance(bts, imports);

            inst.Call("__wasm_call_ctors");
            inst.Call("main",0,0);

            Console.WriteLine("Hello, World!");
        }
    }
}